package com.example.demo.exception;

import lombok.Data;

/**
 * @author LF
 * @date 2021/4/21 - 13:55
 */
@Data
public class ResultBody {
    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应结果
     */
    private Object result;

    public ResultBody() {
    }

    public ResultBody(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.resultCode();
        this.message = errorInfo.resultMsg();
    }

    /**
     * 成功提示
     * @return
     */
    public static ResultBody success() {
        return success(null);
    }

    /**
     * 成功带结果
     * @param data
     * @return
     */
    public static ResultBody success(Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(CommonEnum.SUCCESS.resultCode());
        rb.setMessage(CommonEnum.SUCCESS.resultMsg());
        rb.setResult(data);
        return rb;
    }

    /**
     * 失败消息
     */
    public static ResultBody error(BaseErrorInfoInterface errorInfo) {
        ResultBody rb = new ResultBody();
        rb.setCode(errorInfo.resultCode());
        rb.setMessage(errorInfo.resultMsg());
        rb.setResult(null);
        return rb;
    }

    /**
     * 自定义失败消息和代码
     */
    public static ResultBody error(String code, String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    /**
     * 自定义失败消息
     */
    public static ResultBody error( String message) {
        ResultBody rb = new ResultBody();
        rb.setCode("-9999");
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }


}
